<?php

class Gateway extends Controller {

    function __construct() {
        parent::__construct();
        $this->view->js = array('gateway/js/default.js');
    }

    function index() {
        $this->view->render('gateway/index');
    }

    function run() {
        $i = 0;
        $c = 0;
        foreach ($this->model->smsList() as $key => $value) {
            if ($this->model->verificarTelefono($value['SenderNumber'])) {
                $validar = new postReplies($value['TextDecoded']);
                $validar->_arrayPositive = array('si', 'claro', 'positivo', 'activado', 'confirmado', 'venceremos', 'chavez', 'julio', 'camarada', 'rodilla');
                $validar->_arrayNegative = array('no', 'negativo', 'perderemos', 'capriles', 'biagio', 'mariquera', 'mierda', 'pudranse', 'mueranse', 'equivocado', 'jodan');
                $resultado = $this->model->db->exec("UPDATE `smsd`.`inbox` SET `read` = '1',`respuesta`='" . $validar->run() . "' WHERE `inbox`.`ID` = " . $value['ID']);
                $remplace = array('([^,0-9])');
                $listPhone = preg_replace($remplace, '', $value['TextDecoded']);
                $phone = explode(',', $listPhone);
                foreach ($phone as $key2 => $value2) {
                    $data['voto'] = 1;
                    $this->model->db->exec("UPDATE `eleccionesccc2012`.`cne` SET `voto` = '1' WHERE `cne`.`cedula` = " . $value2);
                    $c++;
                }
            } else {
                $smsd = new sms_inject();
                $smsd->mass_sms('Numero no Registrado en el Sistema', $value['SenderNumber'], $value['RecipientID']);
//                $validar = new postReplies($value['TextDecoded']);
//                $validar->_arrayPositive = array('si', 'claro', 'positivo', 'activado', 'confirmado', 'venceremos', 'chavez', 'julio', 'camarada', 'rodilla');
//                $validar->_arrayNegative = array('no', 'negativo', 'perderemos', 'capriles', 'biaguio', 'mariquera', 'mierda', 'pudranse', 'mueranse', 'equivocado', 'jodan');
//                $resultado = $this->model->db->exec("UPDATE `smsd`.`inbox` SET `read` = '1',`respuesta`='" . $validar->run() . "' WHERE `inbox`.`ID` = " . $value['ID']);
                $resultado = $this->model->db->exec("UPDATE `smsd`.`inbox` SET `read` = '1' WHERE `inbox`.`ID` = " . $value['ID']);
            }
            $i++;
        }
        $data['i'] = $i;
        $data['c'] = $c;
        $inbox = $this->model->inboxStatus();
        foreach ($inbox as $key => $value) {
            $data['inbox'][$value['RecipientID']] = $value['count'];
        }

        $outbox = $this->model->outboxStatus();
        foreach ($outbox as $key => $value) {
            $data['outbox'][$value['SenderID']] = $value['count'];
        }

        $sentitems = $this->model->sentitemsStatus();
        foreach ($sentitems as $key => $value) {
            $data['sentitems'][$value['SenderID']] = $value['count'];
        }

        $respuesta = $this->model->respuestaStatus();
        foreach ($respuesta as $key => $value) {
            $data['respuesta'][$value['RecipientID']] = $value;
        }

        print json_encode($data);
    }

    function test() {
        $smsd = new sms_inject();
        for ($i = 1; $i <= 12; $i++) {
            $smsd->mass_sms('mun' . $i, '04269512664', 'mun' . $i);
        }
    }

    function test2() {
        $smsd = new sms_inject();
        foreach ($this->model->test() as $key => $value) {
            if ($value['dispositivo'] != 1) {
                for ($i = 1; $i <= 10; $i++) {
                    $smsd->mass_sms('Municipio: ' . $value['cod_municipio'] . '-' . $value['municipio'], '04265541875', 'mun' . $value['dispositivo']);
                }
            }
        }
    }

    function sendSmsResponsable() {
        foreach ($this->model->sendSmsResponsable() as $key => $value) {
            $smsd = new sms_inject();
            $smsd->mass_sms('Responsable 1x10, reporta desde tu teléfono las personas que han votado por el candidato de la revolución de tu patrulla o no, a este teléfono.', $value['telefono'], 'mun' . $value['dispositivo']);
        }
    }

}